﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp6
{
    public partial class Form1 : Form
    {
        private Stack<TreeNode> navigationHistory = new Stack<TreeNode>();
        private Stack<TreeNode> forwardHistory = new Stack<TreeNode>();

        public Form1()
        {
            InitializeComponent();
            label1.Height = 30;
            directoryTree.BeforeExpand += DirectoryTree_BeforeExpand;
            directoryTree.AfterSelect += DirectoryTree_AfterSelect;
            fileList.Click += listView1_DoubleClick;

            fileList.View = View.Details;
            fileList.Columns.Add("文件名", 200);
            fileList.Columns.Add("大小", 100);
            fileList.Columns.Add("类型", 100);
            fileList.Columns.Add("修改日期", 150);

            string rootPath = "F:\\";
            if (Directory.Exists(rootPath))
            {
                TreeNode rootNode = new TreeNode(rootPath);
                rootNode.Tag = rootPath;
                directoryTree.Nodes.Add(rootNode);
                PopulateTreeView(rootNode);
            }

        }
        private void PopulateTreeView(TreeNode parentNode)
        {
            try
            {
                string[] directories = Directory.GetDirectories(parentNode.Tag.ToString());
                foreach (string directory in directories)
                {
                    TreeNode node = new TreeNode(Path.GetFileName(directory));
                    node.Tag = directory;
                    parentNode.Nodes.Add(node);
                    node.Nodes.Add(""); // 添加一个空节点，以便展开时加载子目录
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误：" + ex.Message);
            }
        }

        private void DirectoryTree_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node.Nodes.Count == 1 && e.Node.Nodes[0].Tag == null)
            {
                e.Node.Nodes.Clear();
                PopulateTreeView(e.Node);
            }
        }

        private void DirectoryTree_AfterSelect(object sender, TreeViewEventArgs e)
        {
            UpdateListViewForSelectedNode(e.Node);
            navigationHistory.Push(e.Node);
        }
        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            if (fileList.SelectedItems.Count > 0)
            {
                string selectedPath = fileList.SelectedItems[0].Tag.ToString();
                try
                {
                    if (File.Exists(selectedPath))
                    {
                        if (selectedPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
                        {
                            Process.Start(selectedPath);
                        }
                        else if (selectedPath.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
                        {
                            Process.Start("notepad.exe", selectedPath);
                        }
                    }
                }
                catch (Exception ex) { 
                }
            }
        }
        private void UpdateListViewForSelectedNode(TreeNode selectedNode)
        {
            fileList.Items.Clear();
            string selectedPath = selectedNode.Tag.ToString();
            string[] files = Directory.GetFiles(selectedPath);
            label1.Text = "当前路径："+selectedPath;

            foreach (string file in files)
            {
                FileInfo fileInfo = new FileInfo(file);
                ListViewItem item = new ListViewItem(fileInfo.Name);
                item.SubItems.Add(fileInfo.Length.ToString());
                item.SubItems.Add(fileInfo.Extension);
                item.SubItems.Add(fileInfo.LastWriteTime.ToString());
                item.Tag = fileInfo.FullName;
                fileList.Items.Add(item);
            }


        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (navigationHistory.Count > 1)
            {
                // 弹出当前节点
                TreeNode current = navigationHistory.Pop();

                // 将当前节点添加到前进历史记录
                forwardHistory.Push(current);

                // 获取前一个节点
                TreeNode previousNode = navigationHistory.Peek();

              //  directoryTree.SelectedNode = previousNode;
                UpdateListViewForSelectedNode(previousNode);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (forwardHistory.Count > 0)
            {
                // 弹出前进历史记录的节点
                TreeNode nextNode = forwardHistory.Pop();

                directoryTree.SelectedNode = nextNode;
                UpdateListViewForSelectedNode(nextNode);

                // 将前进的节点添加回导航历史
                navigationHistory.Push(nextNode);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (directoryTree.SelectedNode != null && directoryTree.SelectedNode.Parent != null)
            {
                directoryTree.SelectedNode = directoryTree.SelectedNode.Parent;
                UpdateListViewForSelectedNode(directoryTree.SelectedNode);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            contextMenuStrip1.Show(this, new Point(0, 0));
        }

    }
}
